package ntd.remote.link;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.hutool.log.StaticLog;
import ntd.action.ActionAbs;
import ntd.action.bo.ActionInfo;
import ntd.action.bo.ActionOutBo;
import ntd.remote.link.bo.MsgBO;
import ntd.remote.link.enums.EnumRemoteOpt;
import ntd.remote.online.ComOnline;
import ntd.remote.online.bo.OnlineInfo;

public class ActionRemoteRegisterAction extends ActionAbs {

    public static final String ACTION_REMOTE_REGISTER_ACTION = "action-remote-register-action";

    @Override
    public ActionInfo info() {
        return new ActionInfo("客户端-remote-register-action", ACTION_REMOTE_REGISTER_ACTION, this);
    }

    @Override
    public ActionOutBo handle() {
        MsgBO msgBO = actionInBo.getData().toBean(MsgBO.class);

        OnlineInfo onlineInfo = ComOnline.getOnlineInfo("appInfo", msgBO.getNetCode());
        if (ObjectUtil.isNull(onlineInfo)) {
            StaticLog.error("设备[{}]不在线，无法注册action[{}]...", msgBO.getNetCode(), msgBO.getData());
            return new ActionOutBo("error");
        }
        JSONObject extendField = onlineInfo.getExtendField();
        extendField.putOpt(EnumRemoteOpt.REGISTER_ACTION.getCode() + "-services", JSONUtil.parseArray(msgBO.getData()));
        ComOnline.online("appInfo", onlineInfo);
        StaticLog.info("设备[{}:{}:{}]Action[{}]注册成功...", onlineInfo.getIp(), onlineInfo.getClientCode(), onlineInfo.getName(), extendField);
        return new ActionOutBo();
    }
}
